<?php

/**
 * Setting 表单模型
 *
 * @author Hua Yang <htmlcook@gmail.com>
 * @package common.models
 */
class Setting extends CFormModel
{
	private static $_defaultCategories = array(
		'system' => array(
			'label' => '系统',
		)
	);

	/**
	 * 当前设置分类
	 *
	 * @var string
	 */
	protected $category;

	/**
	 * 当前设置
	 *
	 * @var array
	 */
	protected $setting;

	/**
	 * 当前设置操作选项
	 *
	 * @var array
	 */
	protected $options = array();

	/**
	 * @param string $category
	 * @throws CException
	 */
	public function __construct($category)
	{
		if (isset(self::$_defaultCategories[$category])) {
			$this->setting = self::$_defaultCategories[$category];
		} elseif (isset(Yii::app()->params['setting'][$category])) {
			$this->setting = (array)Yii::app()->params['setting'][$category];
		} else {
			throw new CException('设置分类未找到');
		}

		if (isset($this->setting['options'])) {
			$this->options = (array)$this->setting['options'];
		}

		$this->category = $category;

		/** @var YOption $option */
		$option = Yii::app()->getOption();
		foreach ($this->options as $key => $params) {
			$this->options[$key]['value'] = $option->get($key, isset($this->options[$key]['default']) ? $this->options[$key]['default'] : null);
		}
	}

	public function __get($name)
	{
		if (isset($this->options[$name])) {
			if (isset($this->options[$name]['value'])) {
				return $this->options[$name]['value'];
			} elseif (isset($this->options[$name]['default'])) {
				return $this->options[$name]['default'];
			} else {
				return;
			}
		} else {
			return parent::__get($name);
		}
	}

	public function __set($name, $value)
	{
		//私有的
		if ($name[0] === '_') {
			return;
		}

		if (isset($this->options[$name])) {
			$this->options[$name]['value'] = $value;
		} else {
			parent::__set($name, $value);
		}
	}

	public function __unset($name)
	{
		if ($name[0] === '_') //私有的
		{
			return;
		}

		if (isset($this->options[$name])) {
			unset($this->options[$name]['value']);
		} else {
			parent::__unset($name);
		}
	}

	/**
	 * 获取当前设置的title
	 *
	 * @return string
	 */
	public function getTitle()
	{
		return isset($this->setting['title']) ? $this->setting['title'] : $this->category;
	}

	/**
	 * 获取设置的选项 用于表单渲染
	 *
	 * @return array
	 */
	public function getOptions()
	{
		$options = $this->options;
		foreach ($options as $name => $params) {
			if ($name[0] === '_') { //私有的
				unset($options[$name]);
			}
		}
		return $options;
	}

	/**
	 * 获取设置分类名
	 *
	 * @return string
	 */
	public function getCategory()
	{
		return $this->category;
	}

	/**
	 * 获取设置选项字段标签
	 *
	 * @return string
	 */
	public function attributeLabels()
	{
		$labels = array();
		foreach ($this->options as $name => $params) {
			$labels[$name] = isset($params['label']) ? $params['label'] : $name;
		}
		return $labels;
	}

	/**
	 * 获取设置选项的字段规则
	 */
	public function rules()
	{
		$rules = array();
		$required = array();
		$typeRuleMap = array(
			'text' => 'length',
			'url' => 'url',
			'date' => 'date',
			'email' => 'email',
			'file' => 'file',
			'number' => 'numerical',
			'textarea' => 'safe',
			'html' => 'filter',
			'select' => 'in',
			'radio' => 'in',
			'check' => 'in',
			'boolean' => 'boolean',
			'bool' => 'boolean',
		);

		foreach ($this->options as $name => $params) {
			$type = isset($params['type']) ? $params['type'] : 'text';

			if (isset($params['require'])) {
				$required[] = $name;
			}

			if (!isset($params['rule'])) {
				$rule = isset($typeRuleMap[$type]) ? $typeRuleMap[$type] : 'length';
			} else {
				$rule = $params['rule'];
			}

			if (is_array($rule)) {
				foreach ($rule as $r => $options) {
					$rules[] = array_merge(array($name, $r), $options);
				}
			} else {
				$ruleOptions = isset($params['ruleOptions']) ? (array)$params['ruleOptions'] : array();
				if ($type === 'html') {
					$ruleOptions['filter'] = array(new CHtmlPurifier(), 'purify');
				}

				if ($rule === 'in') {
					$ruleOptions['range'] = isset($params['data']) ? array_keys((array)$params['data']) : array();
				}
				$rules[] = array_merge(array($name, $rule), $ruleOptions);
			}
		}

		if ($required) {
			array_unshift($rules, array($required, 'required'));
		}

		return $rules;
	}

	/**
	 * 保存设置
	 *
	 * @return boolean
	 */
	public function save()
	{
		if (!$this->validate()) {
			return false;
		}

		if (!isset($this->category)) {
			return false;
		}

		/** @var YOption $option */
		$option = Yii::app()->getOption();
		foreach ($this->options as $name => $params) {
			$option->set($name, $params['value'], false);
		}
		$option->clearCache();
		return true;
	}
}